<template>
  <div class="wenwu-collection">
    <content-header title="文物征集"></content-header>
    <div class="content-wrap"  :class="{'collapse':isCollapse}">
      <h4 class="xui-margin-bottom-xs">{{detail.title}}</h4>
      <div  v-html="detail.content"></div>
      <div class="mask color-primary">
        <span v-if="isCollapse == true" @click="isCollapse = false">显示全部<i class="el-icon-caret-bottom "></i></span>
        <span v-if="isCollapse == false" @click="isCollapse = true">收起<i class="el-icon-caret-top "></i></span>
      </div>
    </div>

    <div class="xui-margin-top-lg">
      <h4>联系我们</h4>
      <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-form">
        <el-form-item class="inline" label="您的姓名：" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item class="inline" :inline="true" label="联系电话：" prop="userphone">
          <el-input v-model="form.userphone"></el-input>
        </el-form-item>
        <el-form-item class="inline" :inline="true" label="电子邮箱：" prop="useremail">
          <el-input v-model="form.useremail"></el-input>
        </el-form-item>
        <el-form-item label="文物说明：" prop="wwinfo">
          <el-input type="textarea" v-model="form.wwinfo" rows="5"></el-input>
        </el-form-item>
        <el-form-item label="文物图片：" prop="img" class="info">
          <div class="info-tips">
            <span>上传</span>
            <span>（只能上传6张每张小于10M）</span>
          </div>
          <el-upload
            ref="pics"
            id="pics"
            action=""
            :multiple="true"
            accept="image/*"
            :class="{'hide':picsUrl.length >= 6}"
            list-type="picture-card"
            :auto-upload="false"
            :limit="6"
            :on-exceed="picsExceed"
            :on-change="picsChange"
            :on-remove="picsRemove"
            >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <div style="text-align: center">
            <el-button v-loading="loading" :disabled="disabled" type="primary" @click="validateAndSubmit('form')">提交</el-button>
            <el-button type="primary" @click="resetForm('form')">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import ContentHeader from "../../../components/widge/content-header";
import { rootsUrl } from "@/utils";
import { getWenwuCollectionList } from "@/api/list";

export default {
  components: { ContentHeader },
  data() {
    return {
      isCollapse: true,
      detail: {},
      picsUrl: [],
      files: [],
      form: {
        username: "",
        userphone: "",
        useremail: "",
        wwinfo: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入您的姓名", trigger: "blur" }
        ],
        userphone: [
          { required: true, message: "请输入您的电话", trigger: "blur" }
        ],
        useremail: [{ message: "请输入您的邮箱", trigger: "blur" }],
        wwinfo: [{ required: true, message: "请填写文物说明", trigger: "blur" }]
      },
      disabled: false,
      loading: false
    };
  },
  computed: {
    uploadUrl() {
      // let root = "http://192.168.110.248:8083";
      // return `${root}/museum/culcollection/save.shtml`;
      return `${rootsUrl[0]}/museum/culcollection/save.shtml`;
    }
  },
  methods: {
    submitArt() {
      // 新建formdata对象
      let data = new FormData();
      // 将数据手动添加到formData对象中
      data.append("username", this.form.username);
      data.append("userphone", this.form.userphone);
      data.append("useremail", this.form.useremail);
      data.append("wwinfo", this.form.wwinfo);
      let pics = this.files;
      // console.log("pics=", pics);
      for (let i = 0; i < pics.length; i++) {
        data.append("pictures", pics[i]);
      }

      // 设置请求头格式
      let config = {
        headers: { "Content-Type": "multipart/form-data" }
      };
      // 通过post方式提交数据。
      this.$axios
        .post(this.uploadUrl, data, config)
        .then(res => {
          this.disabled = false;
          this.loading = false;
          if (res.data.flag != 1) {
            this.$message({
              showClose: true,
              type: "error",
              message: res.data.msg
            });
            return false;
          }
          this.$message({
            showClose: true,
            type: "success",
            message: res.data.msg + "感谢您的信息，我们稍后可能会与您联系。"
          });
          this.$refs.form.resetFields();
          this.$refs.pics.clearFiles();
          this.picsUrl = [];
        })
        .catch(err => {
          this.disabled = false;
          this.loading = false;
        });
    },
    picCondition(file) {
      const isLt10M = file.size / 1024 / 1024 < 10;
      if (!isLt10M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return isLt10M;
    },
    picsChange(file, fileList) {
      this.picsUrl.push(file.url);
      this.files.push(file.raw);
    },
    picsRemove(file) {
      if (!file) {
        return false;
      }
      let index = this.picsUrl.indexOf(file.url);
      if (index > -1) {
        this.picsUrl.splice(index, 1);
      }
      this.files.splice(this.files.indexOf(file.raw), 1);
    },
    validateAndSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.picsUrl < 1) {
            this.$message({
              showClose: true,
              type: "error",
              message: "请选择图集的图片后再上传。"
            });
            return false;
          }
          this.loading = true;
          this.disabled = true;
          this.submitArt();
        } else {
          this.$message({
            showClose: true,
            type: "error",
            message: "请填写相关信息后再上传。"
          });
        }
      });
    },

    picsExceed() {
      this.$message({
        showClose: true,
        type: "error",
        message: "最多上传6张图片"
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    getDetail() {
      getWenwuCollectionList().then(res => {
        this.detail = res.data.recordsList[0];
      });
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.getDetail();
    });
  }
};
</script>

<style lang='less'>
.wenwu-collection {
  .content-wrap {
    overflow: hidden;
    position: relative;
    padding-bottom: 40px;
    .mask {
      position: absolute;
      bottom: 0;
      width: 100%;
      height: 40px;
      line-height: 40px;
      background: #fff;
      text-align: center;
      span {
        cursor: pointer;
      }
    }
  }
  .content-wrap.collapse {
    max-height: 300px;
  }
  .inline {
    width: 33%;
    display: inline-block;
  }

  .el-input__inner {
    border-radius: 0 !important;
  }

  .el-button {
    display: inline-block;
    line-height: 1;
    white-space: nowrap;
    cursor: pointer;
    background: #dbad23;
    border: 1px solid #dbad23;
    color: #fff;
    -webkit-appearance: none;
    text-align: center;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    outline: 0;
    /*margin: 0;*/
    -webkit-transition: 0.1s;
    transition: 0.1s;
    padding: 12px 20px;
    font-size: 14px;
    border-radius: 2px;
    width: 118px;
    height: 37px;
    margin-right: 30px;
  }
  .el-upload--picture-card {
    background-color: #fbfdff;
    border: 1px dashed #c0ccda;
    border-radius: 6px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    width: 96px;
    height: 96px;
    line-height: 96px;
    vertical-align: top;
  }
  .info {
    position: relative;
  }
  .info-tips {
    position: absolute;
    top: 25px;
    left: -96px;
  }
  .info-tips span {
    display: block;
  }
  .info-tips span:first-child {
    font-size: 14px;
    text-align: right;
    margin-right: 20px;
  }
  .info-tips span:last-child {
    font-size: 12px;
    width: 89px;
    line-height: 20px;
    color: #ee7a7b;
    margin-top: -10px;
  }
  #pics.hide {
    .el-upload--picture-card {
      display: none !important;
    }
  }
}
</style>
